package cn.broadsense.biz.service.impl;

import cn.broadsense.biz.convert.ChannelConvert;
import cn.broadsense.biz.domain.ChannelOrg;
import cn.broadsense.biz.dto.ChannelOrgTreeDto;
import cn.broadsense.biz.mapper.ChannelOrgMapper;
import cn.broadsense.biz.service.ChannelOrgService;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author bs
 * @description 针对表【t_channel_org(点位组织架构)】的数据库操作Service实现
 * @createDate 2023-09-18 16:55:55
 */
@Service
public class ChannelOrgServiceImpl extends ServiceImpl<ChannelOrgMapper, ChannelOrg>
        implements ChannelOrgService {

    @Resource
    private ChannelConvert channelConvert;

    @Override
    @Cacheable(value = "cf60",cacheManager = "caffeineCache60",key="#orgCode")
    public List<ChannelOrgTreeDto> orgTree(String orgCode) {
        LambdaQueryWrapper<ChannelOrg> query = new LambdaQueryWrapper<>();
        if (!orgCode.equals("0")) {
            query.like(ChannelOrg::getFullOrgCode, orgCode);
        }
        List<ChannelOrg> list = this.list(query);
        return buildTreeOrg(list, orgCode);
    }


    private List<ChannelOrgTreeDto> buildTreeOrg(List<ChannelOrg> list, String orgCode) {
        if (CollUtil.isEmpty(list)) {
            return new ArrayList<>();
        }
        Map<String, List<ChannelOrg>> parentMap = list.stream().collect(Collectors.groupingBy(ChannelOrg::getParentOrgCode));
        //获取跟节点
        List<ChannelOrg> channelOrgList = new ArrayList<>();
        if ("0".equals(orgCode)) {
            channelOrgList = list.stream().filter(e -> e.getParentOrgCode().equals(orgCode)).collect(Collectors.toList());
        } else {
            channelOrgList = list.stream().filter(e -> e.getOrgCode().equals(orgCode)).collect(Collectors.toList());
        }
        if (CollUtil.isEmpty(channelOrgList)) {
            return new ArrayList<>();
        }
        List<ChannelOrgTreeDto> orgTreeDtoList = channelConvert.toChannelOrgTreeList(channelOrgList);
        buildChildren(orgTreeDtoList, parentMap);
        return orgTreeDtoList;
    }

    private List<ChannelOrgTreeDto> buildChildren(List<ChannelOrgTreeDto> orgTreeDtoList, Map<String, List<ChannelOrg>> parentMap) {
        if (CollUtil.isNotEmpty(orgTreeDtoList)) {
            for (ChannelOrgTreeDto org : orgTreeDtoList) {
                String orgCode = org.getOrgCode();
                List<ChannelOrg> channelOrgList = parentMap.get(orgCode);
                if (CollUtil.isNotEmpty(channelOrgList)) {
                    List<ChannelOrgTreeDto> treeDtos = channelConvert.toChannelOrgTreeList(channelOrgList);
                    List<ChannelOrgTreeDto> treeDtoList = buildChildren(treeDtos, parentMap);
                    org.setChildrenList(treeDtoList);
                }
            }
        }
        return orgTreeDtoList;
    }
}




